package com.xindacloud.sms.console.system.service.impl;

import java.math.BigDecimal;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
import java.util.UUID;

import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.xindacloud.sms.console.result.Result;
import com.xindacloud.sms.console.result.ResultGenerator;
import com.xindacloud.sms.console.system.dto.SmsAccountTransationDTO;
import com.xindacloud.sms.console.system.entity.SmsAccountTransation;
import com.xindacloud.sms.console.system.entity.SmsUser;
import com.xindacloud.sms.console.system.mapper.SmsAccountTransationMapper;
import com.xindacloud.sms.console.system.mapper.SmsUserMapper;
import com.xindacloud.sms.console.system.service.ISmsAccountTransationService;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author czc
 * @since 2019-10-14
 */
@Service
public class SmsAccountTransationServiceImpl extends ServiceImpl<SmsAccountTransationMapper, SmsAccountTransation> implements ISmsAccountTransationService {

	@Autowired
	private SmsUserMapper smsUserMapper;
	@Autowired
	private SmsAccountTransationMapper smsAccountTransationMapper;
	
	@Override
	public Result getAccountBalance(SmsAccountTransationDTO smsAccountTransationDTO) {
		Page<SmsAccountTransationDTO> page=new Page<>(smsAccountTransationDTO.getPageNum(),smsAccountTransationDTO.getPageSize());
		Set<Integer> ids=getUserIds(smsAccountTransationDTO.getOperatorId());
		smsAccountTransationDTO.setOperatorIds(ids);		
		IPage<SmsAccountTransationDTO> ipage=smsAccountTransationMapper.getAccountTransationList(page, smsAccountTransationDTO);
		return ResultGenerator.genSuccessResult(ipage);
	}
	
	Set<Integer> idsList=new HashSet<>();
	public Set<Integer> getUserIds(Integer userId){
		QueryWrapper<SmsUser> queryWrapper=new QueryWrapper<>();
		queryWrapper.eq("parent_user_id", userId);
		List<SmsUser> smsUsers=smsUserMapper.selectList(queryWrapper);
		if(smsUsers!=null) {
			idsList.add(userId);
		for(SmsUser smsUser:smsUsers) {
			getUserIds(smsUser.getUserId());
		}
		}
			return idsList;
	
	}

	
	@Override
	public Result addOrCostAccountBalance(SmsAccountTransationDTO smsAccountTransationDTO) {
		SmsAccountTransation smsAccountTransation=new SmsAccountTransation();
		SmsUser smsUser=smsUserMapper.selectById(smsAccountTransationDTO.getAccountUserId());
		if(smsAccountTransationDTO.getTransationAction().equals("+")) {
			smsUser.setBalance(new BigDecimal(smsUser.getBalance()).add(new BigDecimal(smsAccountTransationDTO.getTransationPrice())).doubleValue());
		}else {
			smsUser.setBalance(new BigDecimal(smsUser.getBalance()).subtract(new BigDecimal(smsAccountTransationDTO.getTransationPrice())).doubleValue());
		}
		smsUserMapper.updateById(smsUser);
		smsAccountTransationDTO.setTransationId(UUID.randomUUID().toString().replaceAll("-", ""));
		try {
			BeanUtils.copyProperties(smsAccountTransationDTO,smsAccountTransation);
		} catch (Exception e) {
			e.printStackTrace();
		}
		
		if(this.saveOrUpdate(smsAccountTransation)){
		    return ResultGenerator.genSuccessResult("保存或修改成功！");
		}else {
			return ResultGenerator.genClientFailResult("保存或修改失败！");
		}
	}


	
}
